package com.stress.testing.plugin.apache.rocketmq;

import com.stress.testing.common.matcher.ClassDescription;
import com.stress.testing.common.matcher.ClassMatcher;
import com.stress.testing.common.matcher.MethodDescription;
import com.stress.testing.common.matcher.MethodMatcher;
import com.stress.testing.common.plugin.AbstractPlugin;

import java.util.ArrayList;
import java.util.List;

public class DefaultMQProducerPlugin extends AbstractPlugin {
    @Override
    public ClassMatcher getClassMatcher() {
        return new ClassMatcher() {
            @Override
            public boolean match(ClassDescription classDescription) {
                return "org.apache.rocketmq.client.producer.DefaultMQProducer".equals(classDescription.getName());
            }

            @Override
            public List<MethodMatcher> getMethodMatchers() {
                List<MethodMatcher> methodMatchers = new ArrayList<>(2);

                methodMatchers.add(new MethodMatcher() {
                    @Override
                    public boolean match(MethodDescription methodDescription) {
                        return methodDescription.getName().startsWith("send");
                    }

                    @Override
                    public String getInterceptor() {
                        return "com.stress.testing.plugin.apache.rocketmq.interceptor.DefaultMQProducerSendInterceptor";
                    }
                });

                methodMatchers.add(new MethodMatcher() {
                    @Override
                    public boolean match(MethodDescription methodDescription) {
                        return "start".equals(methodDescription.getName());
                    }

                    @Override
                    public String getInterceptor() {
                        return "com.stress.testing.plugin.apache.rocketmq.interceptor.DefaultMQProducerStartInterceptor";
                    }
                });

                return methodMatchers;
            }
        };
    }
}
